package dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ListNumbersForPage implements Serializable {

	private String type = "";
	private String condition="";
	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	private int singlePageRow = 1;
	private int currentPage = 0;

	private int totalRow = 0;

	private int totalPate = 0;

	Class classObject = null;

	public List list() {
		try {
			classObject = Class.forName(type);

			Session session = HibernateSessionFactory.getSession();
			Transaction tx = session.getTransaction();
			tx.begin();
			Query query = session.createQuery("from " + this.getType() +" "+condition);
			List listTemp = query.list();

			totalRow = listTemp.size();
			System.out.println("totalRow:::"+totalRow);
			int front = singlePageRow * (currentPage - 1);
			int rear = front + singlePageRow;
			List listResult = new ArrayList();
			listResult=null;
//			System.out.println("front:" + front + ",,rear:" + rear);
			if (front >= 0 && rear <= totalRow&&front<=rear)
				listResult = listTemp.subList(front, rear);
			
			else if(front<totalRow)
				listResult = listTemp.subList(front, totalRow);
			 
			tx.commit();
			session.close();
			// return listTemp;
			return listResult;

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
	public List listReverse() {
		try {
			classObject = Class.forName(type);

			Session session = HibernateSessionFactory.getSession();
			Transaction tx = session.getTransaction();
			tx.begin();
			Query query = session.createQuery("from " + this.getType() +" "+condition);
			List listTemp = query.list();
			Collections.reverse(listTemp);
			totalRow = listTemp.size();
			System.out.println("totalRow:::"+totalRow);
			int front = singlePageRow * (currentPage - 1);
			int rear = front + singlePageRow;
			List listResult = new ArrayList();
			listResult=null;
//			System.out.println("front:" + front + ",,rear:" + rear);
			if (front >= 0 && rear <= totalRow&&front<=rear)
				listResult = listTemp.subList(front, rear);
			
			else if(front<totalRow)
				listResult = listTemp.subList(front, totalRow);
			 
			tx.commit();
			session.close();
			// return listTemp;
			return listResult;

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public ListNumbersForPage() {
		super();
		if (singlePageRow == 0)
			singlePageRow = 1;
		totalPate = totalRow / singlePageRow;
		if (totalRow / singlePageRow != 0)
			totalPate++;
		if (currentPage > totalPate)
			totalPate = currentPage;
		if (currentPage < 0)
			currentPage = 0;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}

	public int getSinglePageRow() {
		return singlePageRow;
	}

	public void setSinglePageRow(int singlePageRow) {
		this.singlePageRow = singlePageRow;
	}

	public int getTotalPate() {
		return totalPate;
	}

	public void setTotalPate(int totalPate) {
		this.totalPate = totalPate;
	}

}
